<!DOCTYPE HTML>
<html>
<head>
	<title>我的个人博客</title>
	<meta charset="utf8">
	<link rel="stylesheet" type="text/css" href="style.css">
	<link rel="icon" type="image/png" href="simbs.png">
</head>
<body>

<div class="top">
	<div class="header">
                <div class="logo">
                        <a href="index.php"><img src="logo.png"/></a>
                </div>
		<div id="container">
			<ul>
<?php
	//调用配置文件config.php
	require_once 'config.php';
	//连接数据库 
	$conn = mysql_connect($host, $dbusername,$dbpassword);
	if(!$conn){
		die("连接失败".mysql_error());
	}
	//设置访问数据库的编码
	mysql_query("set names utf8",$conn) or die(mysql_errno());
	//选择数据库
	mysql_select_db($dbname,$conn) or die(mysql_errno());

	$sql = "select * from catalogs";
	$res = mysql_query($sql,$conn);
	while($row=mysql_fetch_assoc($res)){
		$cid = $row['cid'];
		echo "<li><a href=blogManage.php?list=$cid>{$row['catalog']}</a></li>";
	}
	mysql_free_result($res);
	mysql_close($conn);
?>
			</ul>
		</div>
	</div>
	<div class="single">
<?php
	//调用配置文件config.php
	require_once 'config.php';
	//连接数据库 
	$conn = mysql_connect($host, $dbusername,$dbpassword);
	if(!$conn){
		die("连接失败".mysql_error());
	}
	//设置访问数据库的编码
	mysql_query("set names utf8",$conn) or die(mysql_errno());
	//选择数据库
	mysql_select_db($dbname,$conn) or die(mysql_errno());

	$sql = "select count(id) from blog";
	
	//查记录行数
	$res = mysql_query($sql);
	//取出行数count(id),代入$rowCount
	while($row=mysql_fetch_row($res)){ $rowCount=$row[0];}	
	//释放资源
	mysql_free_result($res);	

	//设置每个页面显示的行数
	$pageSize = 10;
	//计算共有多少个页面 
	$pageCount = ceil($rowCount/$pageSize);
	//设置当前页
	if(!empty($_GET['pageNow'])){
		$pageNow = $_GET['pageNow'];
	}else{
		$pageNow = 1;
	}
	//按时间排序提取10篇文章(考虑是否按导航分类)
	if(empty($_GET['list'])){
	$sql = "select id,title,content from blog order by time desc limit ".($pageNow-1)*$pageSize.",$pageSize";
	}else{
	$cid = $_GET['list'];
	$sql = "select id,title,content from blog where cid=$cid order by time desc limit ".($pageNow-1)*$pageSize.",$pageSize";}
	$res = mysql_query($sql,$conn);
	while($row=mysql_fetch_assoc($res)){
		$id = $row['id'];
		echo "<div id='title'><a href='blogPage.php?id=$id'><h2>{$row['title']}</h2></a></div>";
		echo "<div id='content'><a href='blogPage.php?id=$id'><p>{$row['content']}</p></a></div>";	
	}
	//显示上一页和下一页
	if($pageNow>1){
		$prePage = $pageNow -1;
		echo "<a href='blogManage.php?pageNow=$prePage'>上一页</a>&nbsp;";	
	}
	if($pageNow<$pageCount){
		$nextPage = $pageNow +1;
		echo "<a href='blogManage.php?pageNow=$nextPage'>下一页</a>";
	}
?>
	
	<form action='blogManage.php'>
	<span>
<?php
	//显示当前页和共有多少页
	echo "当前页${pageNow}/共{$pageCount}页";

        //释放资源，关闭数据库
        mysql_free_result($res);
        mysql_close($conn);

?>
	<!--跳转到指定页面-->
		跳转到<input type='text' name='pageNow' style="width:20px;"/>页
		<input type='submit' value='Go' />
	</span>
	</form>
	</div>
	<div class="sidebar">
		<div class="loginSuccess">
			<?php
				session_start();
				if(!empty($_SESSION['name'])){
				echo "当前用户: ".$_SESSION['name']."<br />";
                                echo "<a href='blogList.php'>管理博文</a><br/>";
                                echo "<a href='blogAdd.php'>添加博文</a><br/>";
                                echo "<a href='configManage.php'>管理设置</a><br/>";
				echo "<a href='login.php'>重新登录</a>";
				} else{
				echo "<a href='login.php'>用户登录</a>";
				}
			?>
		</div>
		<div class="latest">最近更新
		<ul>
<?php
	//调用配置文件config.php
	require_once 'config.php';
	//连接数据库 
	$conn = mysql_connect($host, $dbusername,$dbpassword);
	if(!$conn){
		die("连接失败".mysql_error());
	}
	//设置访问数据库的编码
	mysql_query("set names utf8",$conn) or die(mysql_errno());
	//选择数据库
	mysql_select_db($dbname,$conn) or die(mysql_errno());



	//查询最新的10条记录
	$sql = "select title,id from blog order by time desc limit 0,10";
	$res = mysql_query($sql,$conn);

	//打印结果
	while($row=mysql_fetch_assoc($res)){
		$id = $row['id'];
		echo "<li><a href='blogPage.php?id=$id'>{$row['title']}</a></li>";
	}

	//关闭资源和数据库
	mysql_free_result($res);
	mysql_close($conn);
?>
		</ul>
		</div>
	</div>
	<div id="footer">
		<p>Copyright &#169;2012 Powered by <a href="http://code.google.com/p/simbs/">simbs</a> </p>
	</div>
</div>
</div>
</body>
</html>
